package com.healthy.controller;
import com.healthy.common.Res;
import com.healthy.entity.Knowledgecontent;
import com.healthy.service.KnowledgecontentService;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/knowledgecontent")
public class KnowledgecontentController {

    @Autowired
    KnowledgecontentService knowledgecontentService;

    @GetMapping("/list")
    public Res cuLIst (Knowledgecontent knowledgecontent){
        return new Res(1,"成功",knowledgecontentService.cuList());
    }

    @GetMapping("/Select/{problem}")
    public Res cuSelect(@PathVariable("problem") String problem) throws Exception{
        return new Res(1,"cljs",knowledgecontentService.cuSelect(problem));
    }

    @GetMapping("/list/Inside")
    public Res cuInside (Knowledgecontent knowledgecontent){
        return new Res(1,"成功",knowledgecontentService.cuInside());
    }

    @GetMapping("/list/Outside")
    public Res cuOutside (Knowledgecontent knowledgecontent){
        return new Res(1,"成功",knowledgecontentService.cuOutside());
    }

    @PostMapping("/Insert")
    public Res cuInsert(@RequestBody(required = false)Knowledgecontent knowledgecontent){
        return new Res(1,"成功",knowledgecontentService.cuInsert(knowledgecontent));
    }
    @DeleteMapping("/delete/{id}")
    // 获取参数的第二种形式：通过url地址传参，常用于针对单个对象的操作，如删除单个对象
    // 对应前端写法为：this.$axios.delete('/api/user/1')，其中1表示删除id为1的用户
    // 后端注解为：@PathVariable("id")，其中 "id" 与Mapping中的 {id} 相匹配，须同名
    public Res cuDelete(@PathVariable("id") int id) throws Exception {
        int res = knowledgecontentService.cuDeleteByid(id);
        if (res > 0) {
            return new Res(1, "删除成功");
        } else {
            return new Res(0, "删除失败");
        }
    }
    @PatchMapping("/Update")
    public Res cuUpdate(@RequestParam int id,String type,String problem,String answer,String doctor) throws Exception{
        int res =knowledgecontentService.cuUpdateByid(id,type,problem,answer,doctor);
        if (res>0){
            return new Res(1, "修改成功");
        } else {
            return new Res(0, "修改失败");
        }
    }
}
